package cn.edu.nuc.ssm.controller.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.nuc.ssm.controller.page.Pager;
import cn.edu.nuc.ssm.domain.Sysfunction;
import cn.edu.nuc.ssm.service.interfaces.FunService;

@Controller
@RequestMapping("fun")
public class SysFunctionController {
    
	@Autowired
	private FunService sysFunction;
	/**
	 * 进入功能列表(查询)
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.GET)
	public String getFun(@RequestParam(name="pname",defaultValue="") String pname,
			@RequestParam(name="current",defaultValue="1") int current , 
			Model model){
		Pager<Sysfunction> pager = sysFunction.funlist(pname,current);
		model.addAttribute("pager", pager);
		return "sys/fun/list";
	}
	/**
	 * 进入添加功能页面,和添加系统功能
	 * @return
	 */
	@RequestMapping(value="add",method=RequestMethod.GET)
	public String addFun2(){
		return "sys/fun/add";
	}
	@RequestMapping(value="add",method=RequestMethod.POST)
	public String addFun(Sysfunction sysfun,Model model){
		System.out.println(sysfun.getFname());
		int result = sysFunction.insertFun(sysfun);
		if(result == 1){
			System.out.println("添加功能成功。");
			return "redirect:list";
		}else{
	        model.addAttribute("msg","添加功能失败！");
		}
		return "sys/fun/add";
	}
	/**
	 * 更新，（全部进行更新）
	 */
	//获取功能信息
	@RequestMapping(value="detail/{fid}",method=RequestMethod.GET)
	public String updateFun(@PathVariable("fid") Integer fid,Model model){
		Sysfunction sysfun = sysFunction.selectByFid(fid);
		model.addAttribute("sysfun", sysfun);
		return "sys/fun/update";
	}
	//更新
	@RequestMapping(value="update/{fid}",method=RequestMethod.PUT)
	public String updateFun(@PathVariable("fid") Integer fid,Sysfunction sysfun){
		int result = sysFunction.updateByPrimaryKey(sysfun);
		if(result == 1){
			return "redirect:/fun/list";	
		}
		return  "error";
	}
	/**
	 * 删除单个功能信息
	 */
	@RequestMapping(value="delete/{fid}",method=RequestMethod.DELETE)
	public String deleteFun(@PathVariable("fid") Integer fid){
		int result = sysFunction.deleteByPrimaryKey(fid);
		if(result==1){
			return "redirect:/fun/list";	
		}
		return  "error";
	}
}
